package com.thinkgsp.webapp.controller.sale;

import java.util.HashMap;
import java.util.Map;

import net.sf.jasperreports.engine.JRDataSource;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgsp.jxc.model.Bill;
import com.thinkgsp.jxc.model.BillAmount;
import com.thinkgsp.jxc.model.Emp;
import com.thinkgsp.jxc.model.PurchaseStockInBill;
import com.thinkgsp.jxc.model.RejectStockInBill;
import com.thinkgsp.jxc.model.RejectStockOutBill;
import com.thinkgsp.jxc.service.BillService;
import com.thinkgsp.jxc.service.CustomerService;
import com.thinkgsp.jxc.service.InventoryService;
import com.thinkgsp.jxc.service.ServiceException;
import com.thinkgsp.report.JRBillDataSource;
import com.thinkgsp.sys.model.SysAccountPeriod;
import com.thinkgsp.sys.service.NumberRoleService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExtReturn;
import com.thinkgsp.webapp.util.WebUtils;
@Controller
@RequestMapping(value = "/outreject")
public class RejectStockOutBillController extends BaseController{
	@Autowired
	private BillService billService ;
	@Autowired
	private NumberRoleService numberRoleService;
	@Autowired
	private InventoryService inventoryService;
	
	@Autowired
	private CustomerService customerService;
	
	
	@RequestMapping("/query.do")
    @ResponseBody
    public Object  query(@RequestParam java.sql.Date begDate,@RequestParam java.sql.Date endDate,@RequestParam Integer custId,@RequestParam Integer limit,@RequestParam Integer start){
		try {
			return this.billService.findRejectStockOutBill(begDate, endDate,custId,start,limit);
		} catch (Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn(false,"查询出库退货单出错",e);
		}
	}
	@RequestMapping("/queryNoPaging.do")
    @ResponseBody
    public Object  queryNoPaging(@RequestParam java.sql.Date begDate,@RequestParam java.sql.Date endDate,@RequestParam Integer custId){
		try {
			return this.billService.findRejectStockOutBill(begDate, endDate,custId);
		} catch (Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn(false,"查询出库退货单出错",e);
		}
	}
	@RequestMapping(value="/archive.do")
	@ResponseBody
	public Object archiveBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			this.billService.archiveBill(billId);
			return new ExtReturn(true,"单据归档成功!");
		} catch(Exception e) {
			loger.debug(e.getMessage());
			return new ExtReturn(false,"单据归档失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/unArchive.do")
	@ResponseBody
	public Object unArchiveBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			this.billService.unArchiveBill(billId);
			return new ExtReturn(true,"单据反归档成功!");
		} catch(Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn(false,"单据反归档失败,原因:" + e.getMessage());
		}
	}
	@RequestMapping(value = "/getpdfReport.do", method = RequestMethod.GET)  
    public ModelAndView doReportPDF(@RequestParam Long billId){  
		if(billId == null || billId == 0L) {
			return null;
		}
        Bill bill =this.billService.getBill(billId);
        JRDataSource datasource = new JRBillDataSource(bill);  
        Map<String,Object> parameterMap = new HashMap<String,Object>();  
          
 
        parameterMap.put("datasource", datasource);  
        parameterMap.put("title",  WebUtils.BILL_TITLE);

        ModelAndView modelAndView = new ModelAndView("salesStockOutBillReport", parameterMap);  
        return modelAndView;  
    }
	
	@RequestMapping(value = "/getpdfReportBlank.do", method = RequestMethod.GET)  
    public ModelAndView doSalesReportBlankPDF(@RequestParam Long billId){  
		if(billId == null || billId == 0L) {
			return null;
		}
        Bill bill =this.billService.getBill(billId);
        JRDataSource datasource = new JRBillDataSource(bill);  
        Map<String,Object> parameterMap = new HashMap<String,Object>();  
          
 
        parameterMap.put("datasource", datasource);  
        parameterMap.put("title", WebUtils.BILL_TITLE);

        ModelAndView modelAndView = new ModelAndView("salesStockOutBillReportBlank", parameterMap);  

        return modelAndView;  
    }
	
	@RequestMapping(value="/delete.do")
	@ResponseBody
	public Object deleteBill(@RequestParam Long billId) {
		if(billId == null || billId == 0L) {
			return new ExtReturn("参数无效。");
		}
		try {
			
			this.billService.deleteBill(billId);
			return new ExtReturn(true,"单据删除成功!");
		} catch(Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn(false,"单据删除失败,原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/getBill.do")
	@ResponseBody
	public Object getBill(@RequestParam Long billId) {
		try {
			return this.billService.getBill(billId);
		} catch(Exception e) {
			loger.error(e.getMessage());
			return new ExtReturn("获取出库退货单出错:"+e.getMessage());
		}
	}
	
	@RequestMapping(value="/save.do" ,method = RequestMethod.POST )
	@ResponseBody
	public Object saveBill(Model model ,@RequestBody RejectStockOutBill bill) {
		if(bill == null) {
			return new ExtReturn("单据无效。");
		}
		try {
			BillAmount amount=bill.recalcBillAmount();
			
			this.billService.saveBill(bill);
			return new ExtReturn(true,"单据保存成功!",bill);
		} catch(Exception e) {
			return new ExtReturn("保存出库退货单失败，原因:" + e.getMessage());
		}
	}
	
	@RequestMapping(value="/createBill.do",method=RequestMethod.GET)
	@ResponseBody
	public Object createBill() {
		RejectStockOutBill bill=new RejectStockOutBill();
		SysAccountPeriod period = this.billService.getCurrentPeriod();
		bill.setFyear(period.getFyear());
		bill.setPeriod(period.getPeriod());
		Emp emp = billService.getCurrentEmp();
		bill.setEmp(emp);
		bill.setMaker(emp);
		bill.setDept(billService.getCurrentUser().getLogonDept());
		try {
			String billCode = numberRoleService.newNumber(13);
			bill.setBillCode(billCode);
		} catch(ServiceException e) {
			Log.error("获取出库退货单编号出错:" + e.getMessage());
		}
		return bill;
	}
}

